package nio.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioClient {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 9999;
    public static void main(String[] args) throws IOException {
        SocketChannel channel = SocketChannel.open();
        // 设置非阻塞
        channel.configureBlocking(false);
        // 连接服务器
        if (!channel.connect(new InetSocketAddress(HOST, PORT))) {
            while (!channel.finishConnect()) {
                System.out.println("连接未完成...");
            }
        }
        // 连接成功, 向通道中写数据
        String msg = "hello...";
        channel.write(ByteBuffer.wrap(msg.getBytes()));
        System.out.println("发送数据成功...");
        System.in.read();
    }
}
